<template>
    <Modal v-model="visible1" title="查看救助政策文件" width="700" style="position:relative;" :key="modalKey">
        <div class="pdf">
            <pdf ref="pdf"
                 v-for="i in pageNum"
                 :key="i"
                 :page="i"
                 :src="pdfSrc"
                 @num-pages="pageTotalNum=$event"
                 @page-loaded="pageLoaded($event)"></pdf>
        </div>
        <div slot="footer">
            <!--            <Button type="primary" @click.stop="prePage">上一页</Button>-->
            <!--            {{pageNum}}/{{pageTotalNum}}-->
            <!--            <Button type="primary" @click.stop="nextPage">下一页</Button>-->
            <Button @click="visible1 = false" type="primary">关闭</Button>
        </div>
    </Modal>
</template>

<script>
    import pdf from 'vue-pdf'
    import CMapReaderFactory from "vue-pdf/src/CMapReaderFactory";

    export default {
        name: 'pdfModal',
        components: {
            pdf
        },
        data() {
            return {
                modalKey: '',
                yjah0004Url: '',
                pdfSrc: '',
                pageNum: 1,
                pageTotalNum: 1,
                visible1: false,
            }
        },
        methods: {
            showFile(yjah0004) {
                // let da = yjah0004;//base64文件流
                // let datas = 'data:application/pdf;base64,' + da
                // this.pdfSrc = pdf.createLoadingTask({ url: datas, CMapReaderFactory });
                this.pdfSrc = yjah0004
                this.visible1 = true;
            },
            pageLoaded(e) {
                this.curPageNum = e
                for (let i = 1; i < this.pageTotalNum; i++) {
                    if (i == this.pageTotalNum) {
                        break
                    }
                    this.nextPage()
                }
            },
            prePage() {
                var p = this.pageNum
                p = p > 1 ? p - 1 : this.pageTotalNum
                this.pageNum = p
            },
            nextPage() {
                var p = this.pageNum
                if (p < this.pageTotalNum) {
                    p = p + 1
                } else {

                }

                // p = p < this.pageTotalNum ? p + 1 : 1
                this.pageNum = p
            }
        },
    }
</script>
